pub trait Luhn {
    fn valid_luhn(&self) -> bool;
}

/// Here is the example of how to implement custom Luhn trait
/// for the &str type. Naturally, you can implement this trait
/// by hand for the every other type presented in the test suite,
/// but your solution will fail if a new type is presented.
/// Perhaps there exists a better solution for this problem?
impl<T: ToString> Luhn for T {
    fn valid_luhn(&self) -> bool {
        let string = self.to_string();
        let tmp = string.chars().filter(|&c| c != ' ').map(|v| v).collect::<Vec<char>>();
        if tmp.is_empty() || tmp.len() < 2 { return false; }
        let li = tmp.iter().rev().enumerate();
        let mut sum = 0_u32;
        for (index, &c) in li {
            if c < '0' || c > '9' {
                return false;
            } else {
                let result: u32 = c.to_digit(10).unwrap();
                let idx = index + 1;
                if idx % 2 == 0 {
                    let i = result * 2;
                    if i >= 10 {
                        let i1 = i % 10;
                        let i2 = i / 10;
                        sum = sum + i1 + i2;
                    } else {
                        sum = sum + i;
                    }
                } else {
                    sum = sum + result;
                }
            }
        }
        return !tmp.is_empty() && sum % 10 == 0;
    }
}
